<?php

namespace mbilling\BillingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use mbilling\BillingBundle\Model\DomainsZonesGatewaysModel;
use mbilling\BillingBundle\Model\UsersAdminLogsModel;
use mbilling\BillingBundle\Entity\domainsZonesGateways;
use mbilling\BillingBundle\Entity\usersAdminLogs;

use mbilling\BillingBundle\Form\DomainsZonesGatewaysForm;

class DomainsZonesGatewaysController extends Controller
{
    public function indexAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $gateway_model = new DomainsZonesGatewaysModel($em);
        $gateways = $gateway_model->findAll();
        return $this->render('BillingBundle:Domains_zones_gateways:index.html.twig', array('gateways' => $gateways));
    }
    public function addAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $gateway_model = new DomainsZonesGatewaysModel($em);
        
        $gateway = new domainsZonesGateways();
        //$gateway_model->create($gateway->initDefaultEntity());//создание по умолчанию
        //return new RedirectResponse($this->generateUrl('domains_zones_gateways_show'));

        $dir=opendir(dirName('../'.basename(__FILE__)).'/src/mbilling/BillingBundle/GatewayScripts/');
        $file_string = array();
        while ($file = readdir($dir))
        {
            if (in_array($file,array('.','..')))
                continue;
            $expl=explode('.', $file);
            $expl_size=sizeof($expl);
            if (mb_strtolower($expl[$expl_size-1],'UTF-8') != 'php')
                continue;
            $file_len = sizeof($file_string);
            $file_string[$file_len] = $file;
        }
        closedir($dir);
        array_multisort($file_string);

        $form = $this->createForm(new DomainsZonesGatewaysForm($file_string), $gateway);
        
        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $gateway->setFile($file_string[$gateway->getFile()]);
                $gateway_model->create($form->getData());

                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('6');//страница гейтвеи доменных зон
                $log->setObjectId($gateway->getId());//изменяемый объект
                $log->setActionTypeId('1');//создание гейтвея
                $log_model->create($log);

                return new RedirectResponse($this->generateUrl('domains_zones_gateways_show'));
            }
        }
        return $this->render('BillingBundle:Domains_zones_gateways:create.html.twig', array('form' => $form->createView()));
    }
    public function updateAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $gateway_model = new DomainsZonesGatewaysModel($em);
        $gateway = $gateway_model->findByPK($id);
        
        $securityContext = $this->container->get('security.context');

        $dir=opendir(dirName('../'.basename(__FILE__)).'/src/mbilling/BillingBundle/GatewayScripts/');
        $file_string = array();
        $index = 0;
        while ($file = readdir($dir))
        {
            if (in_array($file,array('.','..')))
                continue;
            $expl=explode('.', $file);
            $expl_size=sizeof($expl);
            if (mb_strtolower($expl[$expl_size-1],'UTF-8') != 'php')
                continue;
            $file_len = sizeof($file_string);
            $file_string[$file_len] = $file;
            if ($gateway->getFile() == $file)//чтобы автоматически выбирался файл в select
                $index = $file_len;
        }
        closedir($dir);
        array_multisort($file_string);

        $gateway->setFile($index-1);
        $form = $this->createForm(new DomainsZonesGatewaysForm($file_string), $gateway);
        
        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $gateway->setFile($file_string[$gateway->getFile()]);
                $gateway_model->create($form->getData());
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('6');//страница гейтвеи доменных зон
                $log->setObjectId($gateway->getId());//изменяемый объект
                $log->setActionTypeId('2');//обновление зоны
                $log_model->create($log);

                return new RedirectResponse($this->generateUrl('domains_zones_gateways_show'));
            }
        }
        return $this->render('BillingBundle:Domains_zones_gateways:create.html.twig', array('form' => $form->createView()));
    }
    public function removeAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $gateway_model = new DomainsZonesGatewaysModel($em);

        $gateway = $gateway_model->findByPK($id);
        $securityContext = $this->container->get('security.context');

        $gateway_model->removeByPK($id);
        $userCurrent = $securityContext->getToken()->getUser();
        $log_model = new UsersAdminLogsModel($em);
        $log = new usersAdminLogs();
        $log->setAdminId($userCurrent->getId());
        $log->setPageId('6');//страница гейтвеи доменных зон
        $log->setObjectId($id);//изменяемый объект
        $log->setActionTypeId('3');//удаление зоны
        $log_model->create($log);
            
        return new RedirectResponse($this->generateUrl('domains_zones_gateways_show'));
    }
}